package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118607769";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjdapCUKTij60DRqqnUlA0NR67IRIZV1p3iCQkteuwot3Pq3Nys8u2ZAm0M7r+WzmA91FgyAs2MvcZvSeWWiq4EIjjj0wYYfZXlYRoB01DvlJ8UqiIsActioRF/xeUJGQIfC+BV98UtUpLrL8kVDMiD7z9Gqn7+NMKkNNORpUUvfb/TnmpqjI92dXt5qqAxP/ckqNM/cd1DgZVb3n2iAPLzUvswujoQBMa53uYZCl89zhJY6IU5M3ROdAgrFqWSTJysuEHZNAwkdaC5meaYwt3j2I/jsjsJw2IATCUizPK7pujlhZVJP/CRYY3e+ma9bGNAgar08Uc/VC5tV/OAYatAgMBAAECggEAAUMVT3HD9CPb2ByhkK0oBV8rn5I/y4ZLIoUgkk/9Q93eDI5sU8RjnELleEi19uTJat9991tUCtUV51L4jUag8c7e37LpiX6oIkD7RrpXtGAxztfsiTGivI1HPCseCXzGcPJUg5EkHcrQigkJaMRlqKuqb+HW0iU3q+s2VnJhEEnsA3ETDWkegzPksOCQc2nG/a1vcyqC0ckVIT/GeWD+HoeT6eo3aZr1tLpLyfn1UT3HDQ29H80EDf5kuR2WWzXg9hxq2IVYpz8f4NJDZ7mm1PSsOwYevPdhc0NjBcAKY5+zJKgwr76nbXGb+8J9pP5NzTZjBibDAfOvsX5iZymkWQKBgQDic6p/RBtVmpedv6TNxvvYk/dqtjP5pxEgTKNgItIx7AdyjLoX8BOmBc+3DxLh2W/gsuqPksm2X+wkQxwxzCtaJAWSXBfdVeNuMCkeEVP2A9yipXBNPMXGLcwRBDiq52jM+BeUsD+US+38ebCX62+o5QVUUGTEzACwSONnZOT9YwKBgQC4ydNJE4LCKqRsaE9bOCcNQEu5zXLKnnrq7QTnqXtiz7yEosBNhKImaRN1eUFyirCuL+AxGM1AawFrWuBdAKNSv064cIQBu20UXcxZux361hlblOHkXl8LU7Z/HXHU8qfSiW2jEX+kE6rM19zSc9mdpNsvjiKGiEm1F7zGC/9wrwKBgAVc19zLsBoG3zBNKYn1/GDUoDdWuURgQHeTWQsdx/yGqmZ8Dqwic1SrLpAXO0s96vhXRFbCEyTOgLkbHb/QJfasg7VDmQ0Uy54AZfjgUCyzSrZPVKl/lu9qhjqg7vadJE42Udijk93fLnOcLiJq4OoPAmFMxhYi/qEuGLSKHyIhAoGBAJ9OwCyb+XrTsHUVaHTH8ELoT2Cb0n8EKgwm0re10X3agjgG56rs0a4zoGcCZn4NO0ywUaOCMlF67vpSmmMTjbpk28T+m4+yudN5GhexzaLWuX3O2QA8q8vkG81y4RQpLq2/dRnEmDooCnQXFXUKE25pFd3lt7YsAP9yu3gyF8JZAoGAR302JsK1MAyvYjb6ruvwRIhItRVX0PzlsnmeL3JSCIpGaPNxXySBkndhkqy3c4YFi5TnAkMlRGnB+dNiVXA7gTDoixyNqCvuUYNLZmJnwIz1C2z5MW9Nw2lfhyYpb6f3CefLUT0xfKihumhlBk3wz0vDjZp4kTGpemdShYbdAyY=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu9SMUXPlDSal071sS1bwHhRIkoZitq1RF9UXh4Qk0j8QoQEgPzzWeQLKf42dtmzTqAWlu2XBWYJkrELXZ9cXm58FB1f3drEoL3RSUh522MfTWQmbmIOBlQ9YQwE4xNBrdF1qLnQSM8m+Y3ht6FXMcKaXTg2/GR0zPbHdpc8kQEIhsLOwjuj9ySe000bz3BqY/rAjHb+9Olej4Y7YrKBt6oCLc+ttk37KqKOKIQqa7pvQuf/Mnr9EqaqNcMcazMg4fZTxFZ9XAjRuyvTzMzyAz1hjY7O8GRuXWLs5BrWqQc+KpOC+3FDRVyGkELJz5AtySudrgiQW2VNRdD+0smhZTwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://41t85g8506.zicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://order.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    //订单超时
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
